package com.real.fact.web.product.application;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.real.fact.web.application.AbstractPagedListCommand;
import com.real.fact.web.product.domain.Fields;
import com.real.fact.web.product.domain.Product;
import com.real.fact.web.product.domain.ProductFilter;
import com.real.fact.web.product.domain.ProductRepository;

public class ListProductsCommand extends AbstractPagedListCommand<Product> {

  private final ProductRepository productRepository;
  
  private Map<String, String> searchFields;
  
  private String searchFieldSelected = Fields.NAME.toString();
  
  private boolean activeChecked = true;

  private String field = "";

  public ListProductsCommand(ProductRepository productRepository) {
    super();
    this.productRepository = productRepository;
    searchFields = new HashMap<String, String>();
    searchFields.put(Fields.NAME.toString(), "product.list.name");
    searchFields.put(Fields.CODE.toString(), "product.list.code");
    searchFields.put(Fields.BRAND.toString(), "product.list.brand");
    searchFields.put(Fields.CATEGORY.toString(), "product.list.category");
    searchFields.put(Fields.DESCRIPTION.toString(), "product.list.description");
  }

  public List<Product> execute() {
    ProductFilter filter = new ProductFilter(activeChecked, searchFieldSelected, field);
    return productRepository.filter(getPaging(), filter);
  }

  public String getField() {
    return field;
  }

  public void setField(String name) {
    this.field = name;
  }

  public String getSearchFieldSelected() {
    return searchFieldSelected;
  }

  public void setSearchFieldSelected(String searchFieldSelected) {
    this.searchFieldSelected = searchFieldSelected;
  }

  public boolean isActiveChecked() {
    return activeChecked;
  }

  public void setActiveChecked(boolean activeChecked) {
    this.activeChecked = activeChecked;
  }

  public Map<String, String> getSearchFields() {
    return searchFields;
  }

  public void setSearchFields(Map<String, String> searchFields) {
    this.searchFields = searchFields;
  }

}
